// mine/personal/personal.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
      tel:"",
      region: [],
      range:[
        {
          value:"女"
        },
        {
          value:"男"
        }
      ],
      // 性别 0 1
      sex:0,
      headerImg:"",
      userNickname:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var m_id = app.globalData.user.m_id;
    var that = this;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Member/configMember',
      data: {
        m_id: m_id
      },
      header:{
        "content-type":"application/json"
      },
      success:function(res){
        var tel = res.data.data.account;
        var sex = res.data.data.sex;
        var city = res.data.data.city;
        var headerImg = res.data.data.head_pic;
        var nick_name = res.data.data.nick_name;
        that.setData({
          tel: tel,
          sex: sex,
          region: city,
          headerImg: headerImg,
          userNickname: nick_name
        })

      },
      fail:function(res){
        wx.showModal({
          title: 'title',
          content: res,
          showCancel:false
        })
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var m_id = app.globalData.user.m_id;
    var that = this;
    wx.request({
      url: app.globalData.yuming + 'merchant.php/Api/Member/configMember',
      data: {
        m_id: m_id
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        console.log(res.data.data)
        var tel = res.data.data.account;
        var sex = res.data.data.sex;
        var city = res.data.data.city;
        var headerImg = res.data.data.head_pic;
        var nick_name = res.data.data.nick_name;
        that.setData({
          tel: tel,
          sex: sex,
          region: city,
          headerImg: headerImg,
          userNickname: nick_name
        })

      },
      fail: function (res) {
        wx.showModal({
          title: 'title',
          content: res,
          showCancel: false
        })
      }
    })
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 点击查看头像
  preview:function(){
    wx.previewImage({
      urls: ["https://ss2.baidu.com/6ONYsjip0QIZ8tyhnq/it/u=2136740882,3271518133&fm=58&bpow=630&bpoh=630"],
    })
  },
  // 输入手机号
  puttel:function(e){
    var tel = e.detail.value;
    this.setData({
      tel:tel
    })
  },
  // 选择地址
  chooseValue:function(e){
    var address = e.detail.value;
    this.setData({
      region:address
    })
  },
  // 取消选择地址时触发
  cancelChoose:function(){
   
  },
  // 选择性别
  chooseSex:function(e){
    var sex = e.detail.value;
    console.log(sex)
    this.setData({
      sex:sex
    })
  },
  // 选择头像
  chooseImage:function(){
    var that = this;
    wx.chooseImage({
      count:1,
      sizeType: "original",
      success: function(res) {
        var headerImg = res.tempFilePaths[0];
        wx.uploadFile({
          url: app.globalData.yuming + 'merchant.php/Api/ProductOrder/picUpload',
          filePath: headerImg,
          name: 'pic',
          formData:{
            type:1
          },
          success:function(res){
            var img = JSON.parse(res.data);
            headerImg = img.data;
            that.setData({
              headerImg: app.globalData.yuming + headerImg
            })
          },
          fail:function(res){
            wx.showModal({
              title: '提示',
              content: res.data.message,
              showCancel:false
            })
          }
        })
        
      },
    })
  },
  // 输入昵称
  userNickname:function(e){
    var userNickname = e.detail.value;
    this.setData({
      userNickname: userNickname
    })
  },
  complata:function(){
    var nick_name = this.data.userNickname;
    var sex = this.data.range[this.data.sex].value;
    var account = this.data.tel;
    var city = this.data.region;
    var head_pic = this.data.headerImg;
    var m_id = app.globalData.user.m_id;
    wx.request({
      url: app.globalData.yuming +　'merchant.php/Api/Member/configMember',
      data:{
        nick_name: nick_name,
        sex: sex,
        account: account,
        city: city,
        head_pic: head_pic,
        m_id: m_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        wx.switchTab({
          url: '/pages/mine/mine',
        })
      }
    })
  }
})